<?php

namespace app\admin\controller;

use think\Config;
use think\Controller;
use think\Db;
use \think\Request;
use think\Session;

/**
 * @writer:huangmingshuai
 * @DateTime:2018-2-24
 * @rewriter:xx
 * @rewriter_date:YY-MM-DD
 * @description:用户管理接口
 * @param:
 * @return [$result]
 */
class User extends Common
{

    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 用户列表接口
     * @return \think\response\Json
     */
    public function userList()
    {
        $post = Request::instance()->post();
        $page = $post['pageNumber'] ? $post['pageNumber'] : 1;
        $limit = $post['pageSize'] ? $post['pageSize'] : 10;
        $limit1 = ($page - 1) * $limit . ',' . $limit;
        $where = [];
        if (array_key_exists('district', $post['search']))
            $where['country|city|town'] = ['like', '%' . $post['search']['district'] . '%'];
        if (array_key_exists('name', $post['search']))
            $where['name'] = ['like', '%' . $post['search']['name'] . '%'];
        if (array_key_exists('phone', $post['search']))
            $where['phone'] = ['like', '%' . $post['search']['phone'] . '%'];
        if (array_key_exists('tel', $post['search']))
            $where['tel'] = ['like', '%' . $post['search']['tel'] . '%'];
        if (array_key_exists('user_name', $post['search']))
            $where['user_name'] = ['like', '%' . $post['search']['user_name'] . '%'];
        $info = Db::name('user')
            ->field('id,name,company_name,tel')
            ->where($where)
            ->order('id desc')
            ->limit($limit1)
            ->select();
        $count = Db::name('user')
            ->field('id,name,company_name,tel')
            ->where($where)
            ->count();
        if ($info) {
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        } else {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }

    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 用户详情接口
     * @return \think\response\Json
     */
    public function userDetail()
    {
        $id = Request::instance()->post('id');
        if (!empty($id)) {
            $info = Db::name('user')
                ->field('id,name,postcode,addr,company_name,tel,user_name,phone,email,account,password')
                ->where('id', $id)
                ->find();
            $result = Config::get('code.success');
            $result['data'] = $info;
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 修改用户信息接口
     * @return \think\response\Json
     */
    public function saveUserInfo()
    {
        $data = Request::instance()->post();
        $id = $data['data']['id'];
        unset($data['data']['id']);
        if ($id) {
            if(array_key_exists('password' , $data['data']) && !empty($data['data']['password']))
            {
                $data['data']['password'] = md5($data['data']['password']);
            }
            $status = Db::name('user')->where('id', $id)->update($data['data']);
            if ($status) {
                $result = Config::get('code.success');
            } else {
                $result = Config::get('code.error');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @return \think\response\Json
     */
    public function login()
    {
        $data = Request::instance()->post();
        if(array_key_exists('admin_name' , $data) && array_key_exists('password' , $data))
        {
            $info = Db::name('admin')->where('admin_name',$data['admin_name'])->find();
            if($info)
            {
                $password = md5(md5($data['password'].$info['strict']));
                if($password == $info['password'])
                {
                    Session::set('user_info',$info);
                    $result['msg'] = '登录成功';
                    $result['status'] = 200;
                }
                else
                {
                    $result = Config::get('code.pwdFail');
                }
            }
            else
            {
                $result['msg'] = '该帐号不存在';
                $result['status'] = 203;
            }
        }
        else
        {
            $result = Config::get('code.pwdFail');
        }
        return json($result);
    }

    /**
     * 获取新增商家商品的数量
     * @return \think\response\Json
     */
    public function getMessageCount()
    {
        $data = Request::instance()->post();
        $res = Session::get('num');
        if($data['status'] == 'yes')
        {
            if($res['sup_num'] == 0 && $res['pro_num'] !=0)
            {
                $pro_num = Db::name('product')->where(['status' => 0,'is_shelves'=>3])->count();
                $sup_num = Db::name('supplier')->where('check_status',1)->whereTime('registration_time','between',[$res['time'],date('Y-m-d H:i:s')])->count();
                $total_num = $pro_num + $sup_num;
                Session::set('num',[
                    'sup_num' => $sup_num,
                    'pro_num' => $pro_num,
                    'total_num' => $total_num,
                    'time' => date('Y-m-d H:i:s'),
                ]);
                $result['data'] = [
                    'sup_num' => $sup_num,
                    'pro_num' => $pro_num,
                    'total_num' => $total_num
                ];
            }
            else if($res['pro_num'] == 0 && $res['sup_num'] != 0)
            {
                $sup_num = Db::name('supplier')->where('check_status',1)->count();
                $pro_num = Db::name('product')->where(['status' => 0,'is_shelves'=>3])->whereTime('create_time','between',[$res['time'],date('Y-m-d H:i:s')])->count();
                $total_num = $sup_num + $pro_num;
                Session::set('num',[
                    'sup_num' => $sup_num,
                    'pro_num' => $pro_num,
                    'total_num' => $total_num,
                    'time' => date('Y-m-d H:i:s'),
                ]);
                 $result['data'] = [
                    'sup_num' => $sup_num,
                    'pro_num' => $pro_num,
                    'total_num' => $total_num
                ];
            }
            else
            {
                $sup_num = Db::name('supplier')->where('check_status',1)->whereTime('registration_time','between',[$res['time'],date('Y-m-d H:i:s')])->count();
                $pro_num = Db::name('product')->where(['status' => 0,'is_shelves'=>3])->whereTime('create_time','between',[$res['time'],date('Y-m-d H:i:s')])->count();
                $total_num = $sup_num + $pro_num;
                Session::set('num',[
                    'sup_num' => $sup_num,
                    'pro_num' => $pro_num,
                    'total_num' => $total_num,
                    'time' => date('Y-m-d H:i:s'),
                ]);
                $result['data'] = [
                    'sup_num' => $sup_num,
                    'pro_num' => $pro_num,
                    'total_num' => $total_num
                ];
            }
        }
        else
        {
            $sup_num = Db::name('supplier')->where('check_status',1)->count();
            $pro_num = Db::name('product')->where(['status' => 0,'is_shelves'=>3])->count();
            $total_num = $sup_num + $pro_num;
            Session::set('num',[
                'sup_num' => $sup_num,
                'pro_num' => $pro_num,
                'total_num' => $total_num,
                'time' => date('Y-m-d H:i:s'),
            ]);
            $result['data'] = [
                'sup_num' => $sup_num,
                'pro_num' => $pro_num,
                'total_num' => $total_num
            ];
        }
        return json($result);
    }


   public function clearMessageCount()
   {
      $data = Request::instance()->post();
      if($data['type'] == 'pro')
      {
          $info = Session::get('num');
          $total_num = $info['total_num'] - $info['pro_num'];
          Session::set('num',[
              'sup_num' => $info['sup_num'],
              'pro_num' => 0,
              'total_num' => $total_num,
              'time' => $info['time']
          ]);
      }
      else if($data['type'] == 'sup')
      {
          $info = Session::get('num');
          $total_num = $info['total_num'] - $info['sup_num'];
          $result = Session::set('num',[
              'pro_num' => $info['pro_num'],
              'sup_num' => 0,
              'total_num' => $total_num,
              'time' => $info['time']
          ]);
      }
     $result['data'] = Session::get('num');
     return json($result);
   }

    /**
     * 退出登录
     */
    public function exitLogin()
    {
        Session::delete('user_info');
        Session::delete('num');
        $this->redirect('Index/login');exit;
    }
}